#include "stdafx.h"
#include <math.h>

#include "MathUtils.h"

namespace ZeEngine
{
    const double MathUtils::cPI = 4 * std::atan(1);
    const double MathUtils::cDegToRad = cPI / 180.0f;

    s32 MathUtils::GetAngleABC( const Vector2D<float>& a, const Vector2D<float>& b, const Vector2D<float>& c )
    {
        Vector2D<float> ab ( b.GetX() - a.GetX(), b.GetY() - a.GetY() );
        Vector2D<float> cb ( b.GetX() - c.GetX(), b.GetY() - c.GetY() );

        float dot = (ab.GetX() * cb.GetX() + ab.GetY() * cb.GetY()); // dot product
        float cross = (ab.GetX() * cb.GetY() - ab.GetY() * cb.GetX()); // cross product

        float alpha = atan2(cross, dot);

        return (int) floor(alpha * 180. / cPI + 0.5);
    }

    f32 MathUtils::GetAngleFromRadiansNormalized(float radians)
    {
        float angle = GetAngleFromRadians(radians);

        /*
        while (angle < 0)
        {
            angle += 360;
        }

        while (angle > 360)
        {
            angle -= 360;
        }
        */
        return angle;
    }

    f32 MathUtils::GetAngleFromRadians(float radians)
    {
        return static_cast<f32>((radians * 180.0f) / cPI);
    }

	bool MathUtils::Contains(const Vector2D<float>& position, const Vector2D<float>& testPosition, const Vector2D<float>& testDimension)
	{
		return (position.GetX() >=  testPosition.GetX() &&
			position.GetX() <= (testPosition.GetX() + testDimension.GetX()) &&
			position.GetY() >= testPosition.GetY() &&
			position.GetY() <= (testPosition.GetY() + testDimension.GetY()));
	}
}